﻿using System;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.Text;
using Antix.Mail.Smtp.Impostor;
using Xunit;

namespace Antix.Mail.Tests
{
    /// <summary>
    ///   Summary description for LogTests
    /// </summary>
    public class LogTests
    {
        const string EXCEPTION_TEXT = "A test error occurred";

        [Fact(Skip = "meh, static methods")]
        public void LogException()
        {
            var log = new StringBuilder();

            Trace.Listeners.Clear();
            Trace.Listeners.Add(new TextWriterTraceListener(new StringWriter(log)));

            try
            {
                throw new Exception(EXCEPTION_TEXT);
            }
            catch (Exception ex)
            {
                Log.Error($"LogException {ex}");
            }

            Console.Write(@"LOG OUTPUT--------
{0}
----------------------", log);

            Assert.True(log.Length > 0);
            Assert.True(log.ToString().Contains(EXCEPTION_TEXT));

            Trace.Listeners.Clear();
        }

        [Fact(Skip = "meh, static methods")]
        public void LogExceptionOverridenAction()
        {
            var log = new StringBuilder();

            Log.LogErrorAction = m =>
            {
                log.AppendLine(DateTime.Now.ToString(CultureInfo.InvariantCulture));
                log.AppendLine(m);
            };

            try
            {
                throw new Exception(EXCEPTION_TEXT);
            }
            catch (Exception ex)
            {
                Log.Error($"LogException {ex}");
            }

            Assert.True(log.Length > 0);
            Assert.True(log.ToString().Contains(EXCEPTION_TEXT));
        }
    }
}